package jp.sourceforge.akjrcp.audiomerge;

import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.audiomerge.operation.NewOperation;
import jp.sourceforge.akjrcp.ui.LayoutDataBuilder;
import jp.sourceforge.akjrcp.ui.WidgetBuilder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.ui.operations.IWorkbenchOperationSupport;
import org.eclipse.ui.part.ViewPart;

public class AudioMergeView extends ViewPart{
	public static Log log=LogFactory.getLog(AudioMergeView.class);
	
	public static final String ID = AudioMergeView.class.getName();
	private TableViewer insertAudioDatasTable;
	private InsertAudioDataComposite insertAudioDataComposite;
	
	private IOperationHistory operationHistory;
	
	private List insertAudioDatasList=new ArrayList();
	private AudioInsertDataControler controler;

	private Composite editAreaControls;
	@Override
	public void createPartControl(Composite parent) {
		
		controler=new AudioInsertDataControler();
		;
		operationHistory = getOperationSupport().getOperationHistory();
		//new UndoRedoActionGroup(this.getSite(), getOperationSupport().getUndoContext(), true);


		IActionBars actionBars= getViewSite().getActionBars();
		
		
		
				
		
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
	    
		Action action=new TestUndo();
		// override the superclass implementation of these actions
	  
	    action.setImageDescriptor(
	    		sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
	   
		action.setEnabled(true);
		
		   actionBars.setGlobalActionHandler(
		      ActionFactory.UNDO.getId(),
		      action);	

		IAction ret=actionBars.getGlobalActionHandler(ActionFactory.UNDO.getId());
		System.out.println(ret);
		
		parent.setLayout(new GridLayout());
		Composite editArea=WidgetBuilder.createGridLayoutComposite(parent, SWT.BORDER);
		editArea.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
		
		insertAudioDataComposite=new InsertAudioDataComposite(editArea,SWT.NULL);
		insertAudioDataComposite.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
		editAreaControls = WidgetBuilder.createGridLayoutComposite(editArea, SWT.NULL,4,false);
		Button backDefault=WidgetBuilder.createButton(editAreaControls, "default");
		Button editDefault=WidgetBuilder.createButton(editAreaControls, "edit");
		
		Composite listArea=WidgetBuilder.createGridLayoutComposite(parent, SWT.BORDER);
		listArea.setLayoutData(LayoutDataBuilder.fillBothGridData());
		
		Composite listAreaControls=WidgetBuilder.createGridLayoutComposite(parent, SWT.NULL,4,false);
		Button newButton=WidgetBuilder.createButton(listAreaControls, "new");
		newButton.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e) {
				NewOperation operation=new NewOperation("new",controler,new InsertAudioData());
				operation.addContext(getOperationSupport().getUndoContext());

				try {
					operationHistory.execute(operation, null, null);
				} catch (ExecutionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}});
		Button deleteButton=WidgetBuilder.createButton(listAreaControls, "delete");
		deleteButton.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e) {
				
				
			}});
		insertAudioDatasTable=new TableViewer(listArea,SWT.NULL);
		insertAudioDatasTable.getTable().setLayoutData(LayoutDataBuilder.fillBothGridData());
		
	
	}
	
	public class TestUndo extends RetargetAction{

		public TestUndo() {
			super("hello", "my Undo!");
			setActionHandler(new Action(){
				public void run(){
					try {
						operationHistory.undo(getOperationSupport().getUndoContext(), null	, null);
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				});
		}
		
	}

	private IWorkbenchOperationSupport getOperationSupport() {
		return this.getSite().getWorkbenchWindow().getWorkbench().getOperationSupport();
	}

	@Override
	public void setFocus() {
		editAreaControls.setFocus();
	}
	
	public class AudioInsertDataControler implements DataControler{

		public void add(InsertAudioData data) {
			insertAudioDatasList.add(data);
		}

		public void remove(InsertAudioData data) {
			insertAudioDatasList.remove(data);
		}
		
	}

	public static interface DataControler{
		public void add(InsertAudioData data);
		public void remove(InsertAudioData data);
	}
}
